package com.bibrApp.difu.actvity.jiechu_mvp;

import android.content.Context;
import android.util.Log;

import com.bibrApp.difu.DfApp;
import com.bibrApp.difu.Response.FBInfoResBody;
import com.bibrApp.difu.actvity.others.IOtherView;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.AppManager;
import com.bibrApp.difu.utils.EncryptUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class JiechuPresenter extends BasePresenter<IJiechuView> {
    public JiechuPresenter(Context context, IJiechuView iView) {
        super(context, iView);
    }

    /**
     * 发布借出
     tallaId	String	是	广告Id
     legalQuantity	String	是	法币数量
     tradePassword	String	是	交易密码
     */
    public void jieChuAd(String tallaId ,String legalQuantity,String tradePassword) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("tallaId", tallaId);
        map.put("legalQuantity", legalQuantity);
        map.put("tradePassword", tradePassword);
        RetrofitNet.getInstance()
                .getApi()
                .jieChu(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        iView.success("借出成功");
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        Log.i("ttt", "--------" + msg);
                    }
                });
    }


    /**
     * 查询单个币种资产
     */
    public void querySingleAssets(String coinId) {
        HashMap<String, Object> map = new HashMap<>();
        DfApp dfApp = (DfApp) AppManager.getTopActivity().getApplication();
        map.put("userId", dfApp.getuId());
        map.put("coinId", coinId);
        RetrofitNet.getInstance()
                .getApi()
                .querySingleCionAssets(EncryptUtils.encrypt(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        Log.d("dj","-------"+value);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                        Log.i("ttt", "--------" + msg);
                    }
                });
    }

    /**
     * 获取法币币种信息
     */
    public void getFBInfo() {
        RetrofitNet.getInstance()
                .getApi()
                .getBInfo()
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if(value != null){
                            String jsonStr = EncryptUtils.objectToJson(value);
                            FBInfoResBody fbInfoResBody = new Gson().fromJson(jsonStr,FBInfoResBody.class);
                            if(fbInfoResBody == null)
                                return;
                            iView.successOfGetFBInfo(fbInfoResBody);
                        }

                    }
                });

    }
}
